// GameCode.cpp		Version 11			5/3/08	
// These three functions form the basis of a game loop in the window created in the
// wincode.cpp file

#include "gamecode.h"
#include "mydrawengine.h"
#include "mysoundengine.h"
#include "myinputs.h"
#include <time.h>
#include "gametimer.h"
#include "errorlogger.h"
#include <math.h>
#include "shapes.h"
#include "game.h"

#include "controls.h"
#include "Hub.h"

extern HWND g_Hwnd;				// A handle to the main window application (created and declared in wincode.cpp).
extern HINSTANCE g_hinstance;	// A handle to the program instance
KeyboardBuffer g_KeyboardBuffer;		// Tracks keyboard input

// Your global objects ***********************************************************************
HubControl theHub;
bool hubActive;
Game* pCurrentGame = nullptr;

// Your global functions *********************************************************************


// The game !!! *********************************************************************************

ErrorType GameInit(bool bFullScreen)
// Called once before entering game loop. 
// Use this function to set up the program
// gHwnd is the handle to the window and is required by the constructors for the engines
{
	// Create the engines - this should be done before creating other DDraw objects
	if(FAILED(MyDrawEngine::Start(SCREENWIDTH, SCREENHEIGHT, g_Hwnd, bFullScreen)))
	{
		ErrorLogger::Writeln("Failed to start MyDrawEngine");
		return FAILURE;
	}
	if(FAILED(MySoundEngine::Start(g_Hwnd)))
	{
		ErrorLogger::Writeln("Failed to start MySoundEngine");
		return FAILURE;
	}
	if(FAILED(MyInputs::Start(g_hinstance, g_Hwnd)))
	{
		ErrorLogger::Writeln("Failed to start MyInputs");
		return FAILURE;
	}

	// Your code here
	
	theHub.init();
	srand((unsigned)time);

	return (SUCCESS);
}

// ******************************************************************


ErrorType GameMain()
// Called repeatedly - the game loop
{
	MyDrawEngine* pTheDrawEngine = MyDrawEngine::GetInstance();
	MyInputs* pTheInputs = MyInputs::GetInstance();
	MySoundEngine* pTheSoundEngine = MySoundEngine::GetInstance();

	if (theHub.GetActive())
	{
		theHub.update(pCurrentGame);
		theHub.draw();
	}
	else if (pCurrentGame)
	{
		if(KEYPRESSED(VK_ESCAPE))
		{
			pCurrentGame->setGameActive(false);
		}

		if (!pCurrentGame->getGameActive())
		{
			pCurrentGame->GameShutdown();
			theHub.setActive(true);
			theHub.processGameResult(pCurrentGame->GetResult());
			delete pCurrentGame;
			pCurrentGame = nullptr;

		}
		else
		{
			pCurrentGame->GameMain();
		}
	}

	//Flip and clear the back buffer
	pTheDrawEngine->Flip();
	pTheDrawEngine->ClearBackBuffer();

	// Code to allow user to go full screen or windowed on pressing F1
	if(KEYPRESSED(VK_F1))
		pTheDrawEngine->GoFullScreen();
	if(KEYPRESSED(VK_F2))
		pTheDrawEngine->GoWindowed();

	return SUCCESS;
}

// ***********************************************************

void GameShutdown()
// called after the game loop is finished
{
	// Your code here



	// (engines must be terminated last)
	MyDrawEngine::Terminate();
	MySoundEngine::Terminate();
	MyInputs::Terminate();
}



